// This is the array to be sorted gaps = [701, 301, 132, 57, 23, 10, 4, 1] // Start with the largest gap and work down to a gap of 1 foreach(gap in gaps) { // Do a gapped insertion sort for this gap size. // The first gap elements a[0..gap-1] are already in gapped order // keep adding one more element until the entire array is gap sorted for(i = gap; i < n; i += 1) { // Add gaps[i] to the elements that have been gap sorted // save gaps[i] in temp and make a hole at position i temp = gaps[i] // Shift earlier gap-sorted elements up until the correct location for gaps[i] is found for(j = i; j >= gap and gaps[j - gap] > temp; j -= gap) { gaps[j] = gaps[j - gap] } // Put temp (the original gaps[i]) in its correct location gaps[j] = temp } }
Wikipedia Article: [http://en.wikipedia.org/wiki/Shell_sort]